package com.kimehwa.studyboot.basejava.enumtest;

import java.math.BigDecimal;

/**
 * 枚举的实质是一个类定义
 */
public class MemberDemo {
    public static void main(String[] args) {
        User user = new User(1L, "bravo", MemberEnum.GOLD_MEMBER.getType());
        BigDecimal productPrice = new BigDecimal("1000");
        BigDecimal discountedPrice = calculateFinalPrice(productPrice, user.getMemberType());
        System.out.println(discountedPrice);
    }

    /**
     * 根据会员身份返回折扣后的商品价格
     *
     * @param originPrice
     * @param user
     * @return
     */
    public static BigDecimal calculateFinalPrice(BigDecimal originPrice, Integer type) {
        return MemberEnum.getEnumByType(type).calculateFinalPrice(originPrice);
    }
}


enum MemberEnum{
    GOLD_MEMBER(1," 黄金会员"){
        @Override
        protected BigDecimal calculateFinalPrice(BigDecimal originPrice) {
            return originPrice.multiply(new BigDecimal(("0.6")));
        }
    },
    SILVER_MEMBER(2, "白银会员") {
        @Override
        public BigDecimal calculateFinalPrice(BigDecimal originPrice) {
            return originPrice.multiply(new BigDecimal(("0.7")));
        }
    },
    BRONZE_MEMBER(3, "青铜会员") {
        @Override
        public BigDecimal calculateFinalPrice(BigDecimal originPrice) {
            return originPrice.multiply(new BigDecimal(("0.8")));
        }
    }
    ;

//    -------------- 这里是枚举类的定义
    private final Integer type;
    private final String desc;

    MemberEnum(Integer type,String desc){
        this.type = type;
        this.desc = desc;
    }
    protected abstract BigDecimal calculateFinalPrice(BigDecimal originPrice);

    public Integer getType(){
        return type;
    }

    public String getDesc(){
        return desc;
    }
    public static MemberEnum getEnumByType(Integer type){
        MemberEnum[] values = MemberEnum.values();
        for (MemberEnum memberEnum :
                values) {
            if (memberEnum.getType().equals(type)) {
                return memberEnum;
            }
        }
        throw new IllegalArgumentException("无效的枚举类型");


    }


}


